<?php
/**
 * This class will generate a serie of selectboxes to select a date and time
 *
 * @author Herjan Treurniet
 * @since 2008-08-03
 * @package Form.Elements
 */
class SelectDateTime extends FormElement {

	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param mixed $value
	 * @param array $properties
	 */
	public function __construct( $name, $value = null, array $properties = array() ) {
		if ( empty ( $value ) ) {
			$value = date ( 'Y-m-d H:i:s' );
		}

		$this->value = $this->getDateTimeArray ( $value );

		parent::__construct ( $name, $properties );
	}

	public static function getTimeStamp( array $dateTime ) {
		if ( isset( $dateTime['hour'], $dateTime['minute'], $dateTime['month'], $dateTime['day'], $dateTime['year'] ) ) {
			return mktime ( $dateTime['hour'], $dateTime['minute'], null, $dateTime['month'], $dateTime['day'], $dateTime['year'] );
		}

		return 0;
	}

	public static function getDateTime ( array $dateTime = null ) {
		if ( is_array ( $dateTime ) ) {
			$dateTime = self::getTimeStamp ( $dateTime );
		} else if (ctype_digit((string) $dateTime)) {
			$dateTime = (int) $dateTime;
		} else {
			$dateTime = time();
		}

		return date ( 'Y-m-d H:i:s', $dateTime );
	}

	public function getDateTimeArray ( ) {
		$args = func_get_args ( );

		if ( is_array ( $args [0] ) ) {
			$a = $args [0];
			$string = self::getDateTime($a);
		} elseif ( count ( $args ) == 5 ) {
			$string = $args[0].'-'.$args[1].'-'.$args[2].' '.$args[3].':'.$args[4].':00';
		} else {
			$string = $args [0];
		}

		$time = strtotime ( $string );

		$array = array (
			'year' => date ( 'Y', $time ),
			'month' => date ( 'm', $time ),
			'day' => date ( 'd', $time ),
			'hour' => date ( 'H', $time ),
			'minute' => date ( 'i', $time )
		);

		if ( mktime ( $array['hour'], $array['minute'], 0, $array['month'], $array['day'], $array['year'] ) ) {
			return $array;
		} else {
			return array (
				'year' => date ( 'Y' ),
				'month' => date ( 'm' ),
				'day' => date ( 'd' ),
				'hour' => date ( 'H' ),
				'minute' => date ( 'i' )
			);
		}
	}

	/**
	 * @see FormNode::__toString()
	 */
	public function __toString( ) {
		$select = '<div class="form datetime"><label>' . $this->getProperty("label") . '</label>';

		$selected = $this->value;

		$selected['minute'] = $selected['minute'] - ( $selected['minute'] % 5 );

		$selects = array (
			'day' => range ( 1, 31 ),
			'month' => array ( '##january##', '##february##', '##march##', '##april##', '##may##', '##june##', '##july##', '##august##', '##september##', '##october##', '##november##', '##december##' ),
			'year' => array ( date ( 'Y' ) - 1 => date ( 'Y' ), date ( 'Y' ) => date ( 'Y' ) + 1 ),
			'hour' => array ( -1=>0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ),
			'minute' => array ( -1=>0, 4=>5, 9=>10, 14=>15, 19=>20, 24=>25, 29=>30, 34=>35, 39=>40, 44=>45, 49=>50, 54=>55 )
		);

		foreach ( $selects as $key => $options ) {
			if ( $key == 'hour' ) {
				$select .= ' ##at## ';
			}

			$select .= '<select name="'.( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name.'['.$key.']">';

			foreach ( $options as $identifier => $value ) {
				$select .= '<option value="'.($identifier + 1).'"'.( ( $identifier + 1 ) == $selected[$key] ? ' selected="selected"' : '' ).'>'.$value.'</option>';
			}

			$select .= '</select>';
		}

		$select .= '</div>' . "\r\n";

		return $select;
	}
}
